package com.xky.beetlsql;

import com.xky.beetlsql.configuration.GenConf;
import com.xky.beetlsql.entity.Projectinfo;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.gen.GenConfig;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author keying.xia
 * @date 2017/12/11
 */
public class BeetSqlHelper {

    private static final String driver = "com.mysql.jdbc.Driver";

    private static final String connect_url = "jdbc:mysql://localhost:3306/demo?useSSL=true&autoReconnect=true";

    private static final String account = "root";

    private static final String password = "root";

    public static void main(String[] args) {
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver,connect_url, account, password);

        DBStyle mysql = new MySqlStyle();

        // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql");

        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
        UnderlinedNameConversion nc = new UnderlinedNameConversion();

        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});

        try {
            //根据表名生成pojo类，输出到控制台.
            //sqlManager.genPojoCodeToConsole("projectinfo");

            //生成查询，条件，更新sql模板，输出到控制台
            //sqlManager.genSQLTemplateToConsole("projectinfo");

            //生成pojo到指定位置
            sqlManager.genPojoCode("info","com.xky.beetlsql.entity","D:\\spring\\spring-boot-learning\\beetsql\\src\\main\\java",new GenConfig());


            //根据类来生成对应的sql
            sqlManager.genBuiltInSqlToConsole(Projectinfo.class);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
